var serviceURL = "http://localhost/directorio/services/";

var employees;

$('#employeeListPage').bind('pageinit', function(event) {
    getEmployeeList();
});

function getEmployeeList() {
    $.getJSON(serviceURL + 'getemployees.php', function(data) {
        $('#employeeList li').remove();
        employees = data.items;
        $.each(employees, function(index, employee) {
            $('#employeeList').append('<li><a href="employeedetails.html?id=' + employee.id + '">' +
                    '<img src="pics/' + employee.picture + '"/>' +
                    '<h4>' + employee.firstName + ' ' + employee.lastName + '</h4>' +
                    '<p>' + employee.title + '</p>' +
                    '<span class="ui-li-count">' + employee.reportCount + '</span></a></li>');
        });
        $('#employeeList').listview('refresh');
    });
}

$('#detailsPage').live('pageshow', function(event) {
    var id = getUrlVars()["id"];
    $.getJSON(serviceURL + 'getemployee.php?id=' + id, displayEmployee);
});

function displayEmployee(data) {
    var employee = data.item;
    console.log(employee);
    $('#employeePic').attr('src', 'pics/' + employee.picture);
    $('#fullName').text(employee.firstName + ' ' + employee.lastName);
    $('#employeeTitle').text(employee.title);
    $('#city').text(employee.city);
    console.log(employee.officePhone);
    if (employee.managerId > 0) {
        $('#actionList').append('<li><a href="employeedetails.html?id=' + employee.managerId + '"><h3>View Manager</h3>' +
                '<p>' + employee.managerFirstName + ' ' + employee.managerLastName + '</p></a></li>');
    }
    if (employee.reportCount > 0) {
        $('#actionList').append('<li><a href="reportlist.html?id=' + employee.id + '"><h3>View Direct Reports</h3>' +
                '<p>' + employee.reportCount + '</p></a></li>');
    }
    if (employee.email) {
        $('#actionList').append('<li><a href="mailto:' + employee.email + '"><h3>Email</h3>' +
                '<p>' + employee.email + '</p></a></li>');
    }
    if (employee.officePhone) {
        $('#actionList').append('<li><a href="tel:' + employee.officePhone + '"><h3>Call Office</h3>' +
                '<p>' + employee.officePhone + '</p></a></li>');
    }
    if (employee.cellPhone) {
        $('#actionList').append('<li><a href="tel:' + employee.cellPhone + '"><h3>Call Cell</h3>' +
                '<p>' + employee.cellPhone + '</p></a></li>');
        $('#actionList').append('<li><a href="sms:' + employee.cellPhone + '"><h3>SMS</h3>' +
                '<p>' + employee.cellPhone + '</p></a></li>');
    }
    $('#actionList').listview('refresh');

}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

$('#reportListPage').live('pageshow', function(event) {
    var id = getUrlVars()["id"];
    console.log("reports for " + id);
    $.getJSON(serviceURL + 'getreports.php?id=' + id, function(data) {
        var reports = data.items;
        $.each(reports, function(index, employee) {
            $('#reportList').append('<li><a href="employeedetails.html?id=' + employee.id + '">' +
                    '<h4>' + employee.firstName + ' ' + employee.lastName + '</h4>' +
                    '<p>' + employee.title + '</p>' +
                    '<span class="ui-li-count">' + employee.reportCount + '</span></a></li>');
        });
        $('#reportList').listview('refresh');
    });
});
